﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class ChuyenBayDAO
    {
        Database db;
        SqlConnection sql;
        DataTable dt;
                 
        public ChuyenBayDAO() 
        {
            db = new Database();
            sql = db.getConnection();
        }
        
        public DataTable LayDSChuyenBay()
        {
            dt = db.Execute("Select MACHUYENBAY, MATUYENBAY,CONVERT(char(10),NGAYGIO,103) AS NGAYGIO,KHOIHANH, DEN, THOIGIANBAY,SLGHEHANG1, SLGHEHANG2 FROM CHUYENBAY");
            return dt;
        }
        public void Them(ChuyenBayDTO cbDto)
        {
            String MaCB =cbDto.Machuyenbay;
            String MaTB = cbDto.Matuyenbay;
            DateTime Ngaygio = cbDto.Ngaygio;
            DateTime Khoihanh = cbDto.Khoihanh;
            DateTime Den = cbDto.Den;
            int Tgbay = cbDto.Thoigianbay;
            int hang1 = cbDto.Ghehang1;
            int hang2 = cbDto.Ghehang2;


            String strSQL = "INSERT INTO CHUYENBAY VALUES(" + MaCB + ",N'" + MaTB + "',N'" + Ngaygio + "',N'" + Khoihanh + "',N'" + Den + "',N'" + Tgbay + "',N'" + hang1 + "',N'" + hang2 + "')";
            db.ExecuteNonQuery(strSQL);
                 
        }

        public void Xoa(int index)
        {
            string sql = "delete from CHUYENBAY where MACHUYENBAY ='" + dt.Rows[index]["MACHUYENBAY"].ToString() + "'";//phai nho co dao nhay ' ko thi bi bao loi invalid column name
            db.ExecuteNonQuery(sql);
            dt.Rows.RemoveAt(index);
        }
    }
}
